package com.summer.service;

import com.alibaba.fastjson.JSON;
import com.summer.dao.LecturerDao;
import com.summer.bean.Lecturer;
import com.summer.result.Result;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Description:
 * User: 35391
 * Package: com.summer.service
 * Date: 2025/3/24
 * Time: 下午9:50
 * Version: 1.0
 */
@WebServlet({"/lecturer/selectAllLecturer"})
public class LecturerService extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        Result<List<Lecturer>> result = new Result<>();
        String path = request.getServletPath();
        if ("/lecturer/selectAllLecturer".equals(path)) {
            result = doSelectAllLecturer(request, response);
        }
        if (result != null) {
            String jsonResult = JSON.toJSONString(result);
            response.setContentType("application/json;charset=UTF-8");
            PrintWriter out = response.getWriter();
            out.print(jsonResult);
            out.flush();
            out.close();
        }
    }

    private Result<List<Lecturer>> doSelectAllLecturer(HttpServletRequest request, HttpServletResponse response) {
        List<Lecturer> lecturers = LecturerDao.getAllLecturer();
        return Result.success(lecturers);
    }
}
